package com.itheima.reggie.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.reggie.common.BaseContext;
import com.itheima.reggie.common.R;
import com.itheima.reggie.dto.OrdersDto;
import com.itheima.reggie.entity.Orders;
import com.itheima.reggie.service.OrdersService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;


@RestController
@RequestMapping("/order")
@Slf4j
public class OrdersController {

    @Autowired
    OrdersService ordersService;

    private Map ordersDtoMap = new HashMap<>();
    private Map allOrdersDtoMap = new HashMap<>();

    //用于提交订单
    @PostMapping("/submit")
    public R submit(@RequestBody Orders orders) {
        log.info("订单数据：{}", orders);
        ordersService.submit(orders);
        return R.success("订单添加成功");
    }

    //用于分页查询订单
    @GetMapping("/userPage")
    public R list(int page, int pageSize) {
        log.info("要查第{}页,单页条数{}", page, pageSize);

        ordersDtoMap.clear();

        IPage<Orders> pg = new Page<>(page, pageSize);
        LambdaQueryWrapper<Orders> qw=new LambdaQueryWrapper<>();
        qw.eq(Orders::getUserId, BaseContext.getCurrentId());
        ordersService.page(pg,qw);

        //添加数量
        long total = pg.getTotal();
        ordersDtoMap.put("total", total);

        //获得数据
        List<Orders> orders = pg.getRecords();
        List<OrdersDto> ordersDtos = orders.stream().map((one) -> {
                    OrdersDto ordersDto = new OrdersDto();
                    BeanUtils.copyProperties(one, ordersDto);

                    //对orderDto进行处理
                    ordersService.manage(ordersDto);
                    return ordersDto;
                }
        ).collect(Collectors.toList());

        //添加元素
        ordersDtoMap.put("records", ordersDtos);
        return R.success(ordersDtoMap);
    }


    //用于分页查询订单
    @GetMapping("/page")
    public R allList(int page, int pageSize) {
        log.info("要查第{}页,单页条数{}", page, pageSize);

        allOrdersDtoMap.clear();

        IPage<Orders> pg = new Page<>(page, pageSize);
        LambdaQueryWrapper<Orders> qw=new LambdaQueryWrapper<>();
       // qw.eq(Orders::getUserId, BaseContext.getCurrentId());
        ordersService.page(pg,qw);

        //添加数量
        long total = pg.getTotal();
        allOrdersDtoMap.put("total", total);

        //获得数据
        List<Orders> orders = pg.getRecords();
        List<OrdersDto> ordersDtos = orders.stream().map((one) -> {
                    OrdersDto ordersDto = new OrdersDto();
                    BeanUtils.copyProperties(one, ordersDto);

                    //对orderDto进行处理
                    ordersService.manage(ordersDto);
                    return ordersDto;
                }
        ).collect(Collectors.toList());

        //添加元素
        allOrdersDtoMap.put("records", ordersDtos);
        return R.success(allOrdersDtoMap);
    }

    //用于订单派送
    @PutMapping
    public R go(@RequestBody Orders orders){
        log.info("要派送的单号为{}",orders.getId());
        LambdaUpdateWrapper<Orders> qw=new LambdaUpdateWrapper<>();
        qw.set(Orders::getStatus,orders.getStatus()).eq(Orders::getId,orders.getId());
        ordersService.update(qw);
        return R.success("已派送!");
    }

}
